<?php

namespace App\Http\Controllers\Admin;

use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Response\Response;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

//引用http请求类

class ThemeController extends BaseController
{
    /**
     * @api {Get} /theme/list 话题列表
     * @apiGroup 补充接口-话题广场
     *
     * @apiSuccessExample {json} Response-Example
     * {
    "code": 200,
    "data": {
    "current_page": 1,
    "data": [
    {
    "id": 42,
    "name": "河钢股份",
    "start_time": 1612800000,
    "end_time": 1620489600,
    "chat_room_id": "",
    "is_deleted": 0,
    "create_time": 1620813104,
    "update_time": 1620813104
    },
    {
    "id": 33,
    "name": "",
    "start_time": 1620230405,
    "end_time": 1620835205,
    "chat_room_id": "146290588581889",
    "is_deleted": 0,
    "create_time": 1620230405,
    "update_time": 1620230405
    }
    ],
    "first_page_url": "http://local.qipaoadmin/theme?page=1",
    "from": 1,
    "last_page": 4,
    "last_page_url": "http://local.qipaoadmin/theme?page=4",
    "next_page_url": "http://local.qipaoadmin/theme?page=2",
    "path": "http://local.qipaoadmin/theme",
    "per_page": 10,
    "prev_page_url": null,
    "to": 10,
    "total": 36
    }
    }
     */
    public function list(Request $request)
    {
        $pageLimit = $request->input('per_page', 10);
        $data = app('model_theme')->getListPaging([], ['id', 'name', 'start_time', 'end_time', 'create_time'], $pageLimit);
        foreach ($data as &$v) {
            $v->start_time = date('Y-m-d H:i:s', $v->start_time);
            $v->end_time = date('Y-m-d H:i:s', $v->end_time);
            $v->create_time = date('Y-m-d H:i:s', $v->create_time);
        }
        return Response::sendData($data);
    }

    /**
     * @api {Post} /theme/add 新增话题
     * @apiGroup 补充接口-话题广场
     *
     * @apiParam {String} theme_name 话题名称
     * @apiParam {String} start_time 开始时间，yyyy-mm-dd hh:ii:ss
     * @apiParam {String} end_time 结束时间，yyyy-mm-dd hh:ii:ss
     *
     * @apiSuccessExample {json} Response-Example
     * {
     *   "code":200,
     *   "data":
     * }
     */
    public function add(Request $request)
    {
        $themeName = $request->theme_name;
        $startTime = strtotime($request->start_time);
        $endTime = strtotime($request->end_time);

        $this->paramVerify($themeName, $startTime, $endTime);

        $res = app('model_theme')->add([
            'name' => $themeName,
            'start_time' => $startTime,
            'end_time' => $endTime,
        ]);
        if ($res) {
            return Response::sendData($res);
        } else {
            throw new BasicException(0, '添加失败');
        }
    }

    /**
     * @api {Put} /theme/edit 编辑话题
     * @apiGroup 补充接口-话题广场
     *
     * @apiParam {Int} id 话题编号
     * @apiParam {String} theme_name 话题名称
     * @apiParam {String} start_time 开始时间，yyyy-mm-dd hh:ii:ss
     * @apiParam {String} start_time 结束时间，yyyy-mm-dd hh:ii:ss
     *
     * @apiSuccessExample {json} Response-Example
     * {
     *   "code":200,
     *   "data":
     * }
     */
    public function edit(Request $request)
    {
        $id = $request->id;
        $themeName = $request->theme_name;
        $startTime = strtotime($request->start_time);
        $endTime = strtotime($request->end_time);
        $this->paramVerify($themeName, $startTime, $endTime);

        $res = app('model_theme')->edit($id, [
            'name' => $themeName,
            'start_time' => $startTime,
            'end_time' => $endTime,
        ]);
        if ($res) {
            return Response::sendData([]);
        } else {
            throw new BasicException(0, '编辑失败');
        }
    }

    private function paramVerify($themeName, $startTime, $endTime)
    {
        if (empty($themeName)) throw new BasicException(0, '话题名称不能为空');
        if (mb_strlen($themeName) > 255) throw new BasicException(0, '话题名称不能超过255个字符');
        if (empty($startTime) || empty($endTime)) throw new BasicException(0, '开始和结束时间不能为空');
        if ($startTime > $endTime) throw new BasicException(0, '开始时间不能大于结束时间');
    }

    /**
     * @api {Delete} /theme/del 删除话题
     * @apiGroup 补充接口-话题广场
     *
     * @apiParam {Int} id 话题编号
     *
     * @apiSuccessExample {json} Response-Example
     * {
     *   "code":200,
     *   "data":
     * }
     */
    public function del(Request $request)
    {
        $id = $request->id;
        $res = app('model_theme')->del($id);
        if ($res) {
            return Response::sendData([]);
        } else {
            throw new BasicException(0, '删除失败');
        }
    }

}